package cn.xgj.sfzy.service.impl;

import cn.xgj.sfzy.entity.Charge;
import cn.xgj.sfzy.entity.ChargeType;
import cn.xgj.sfzy.entity.Consult;
import cn.xgj.sfzy.mapper.ChargeMapper;
import cn.xgj.sfzy.mapper.ChargeTypeMapper;
import cn.xgj.sfzy.mapper.ConsultMapper;
import cn.xgj.sfzy.service.ChargeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author 张卫姣
 * @Date 2024年10月24日 16:14
 * @Version 1.0
 */
@Service
public class ChargeServiceImpl implements ChargeService {
    @Autowired
    private ChargeMapper chargeMapper;

    //收费类型对象
    @Autowired
    private ChargeTypeMapper chargeTypeMapper;

    //咨询师对象
    @Autowired
    private ConsultMapper consultMapper;


    @Override
    public Integer add(Charge charge) {
        return chargeMapper.insert(charge);
    }

    @Override
    public Integer remove(Integer chargeId) {
        return chargeMapper.delete(chargeId);
    }

    @Override
    public Integer edit(Charge charge) {
        return chargeMapper.update(charge);
    }

    @Override
    public List<Charge> findAll(Charge charge) {
        return chargeMapper.selectAll(charge);
    }

    @Override
    public Charge findById(Integer chargeId) {
        return chargeMapper.selectById(chargeId);
    }

    @Override
    public PageInfo<Charge> findByPage(Integer pageNum, Integer pageSize, Charge charge) {
        PageHelper.startPage(pageNum,pageSize);
        List<Charge> charges = chargeMapper.selectAll(charge);
        //遍历集合，根据每一个charge的chargeTypeId来查询对应的收费类型对象，并赋值给当前对象
        for (Charge ch : charges){
            //根据收费分类id获取收费分类对象
            Integer chargeTypeId = ch.getChargeTypeId();
            ChargeType chargeType = chargeTypeMapper.selectById(chargeTypeId);
            ch.setChargeType(chargeType);
            //根据咨询师id获取咨询师对象
            Integer consultId = ch.getConsultId();
            Consult consult = consultMapper.selectById(consultId);
            ch.setConsult(consult);

        }

        PageInfo<Charge> pageInfo = new PageInfo<Charge>(charges);

        return pageInfo;


    }
}
